package com.yantu.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yantu.bean.Subject;
import com.yantu.bean.UserEventLog;
import com.yantu.mapper.SubjectMapper;
import com.yantu.mapper.UserEventLogMapper;
import com.yantu.util.RedisUtils;
import com.yantu.util.Result;
import com.yantu.view.admin.AdminPageView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 综合实训课程设计
 * @since 2022-08-13
 */
@RestController
@RequestMapping("/admin")
@Api("管理端学科接口")
public class AdminSubjectController extends AdminUserEventLogController{
    @Autowired
    SubjectMapper subjectMapper;

    @Autowired
    RedisUtils redisUtils;
    //学科列表
    @GetMapping("/subjectList")
    @ApiOperation("学科列表")
    public Result myListsSubject(){
        if(redisUtils.haskeysf("adminSubjectList")){
            System.out.println("缓存里面有");
            List<Subject> subjectList = (List<Subject>) redisUtils.getValue("adminSubjectList");
            return Result.succ(200, "查询成功", subjectList);
        }else {
            System.out.println("缓存里面没有");
            List<Subject> subjectList = subjectMapper.selectList(null);
            redisUtils.saveValue("subjectList", subjectList);
            return Result.succ(200, "查询成功", subjectList);
        }
    }


    //学科分页
    @GetMapping("/subjects")
    @ApiOperation("学科分页信息")
    public Result myListSubject(@ModelAttribute AdminPageView pageView){
        PageHelper.startPage(pageView.getPageIndex(), pageView.getPageSize());
        if(redisUtils.haskeysf("adminSubjectList"+pageView)) {
            System.out.println("缓存里面有");
            PageInfo pageInfo = (PageInfo) redisUtils.getValue("adminSubjectList"+pageView);
            return Result.succ(200, "查询成功", pageInfo);
        }else {
            System.out.println("查数据库");
            QueryWrapper<Subject> queryWrapper = new QueryWrapper<Subject>();
            if (pageView.getLevel() != null) {
                queryWrapper.eq("level", pageView.getLevel());
            }
            List<Subject> subjectList = subjectMapper.selectList(queryWrapper);
            List<Object> subList = new ArrayList<>();
            for (Subject subject : subjectList) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("id", subject.getId());
                map1.put("name", subject.getName());
                map1.put("level", subject.getLevel());
                map1.put("level_name", subject.getLevelName());
                subList.add(map1);
            }
            PageInfo pageInfo = new PageInfo(subList);
            redisUtils.saveValue("adminSubjectList"+pageView, pageInfo);
            return Result.succ(200, "查询成功", pageInfo);
        }
    }

    //学科查询
    @GetMapping("subject/{id}")
    @ApiOperation("根据学科id查询学科信息")
    public Result getSubjectById(@PathVariable("id") Integer id){
        if(redisUtils.haskeysf("adminSubject"+id)) {
            System.out.println("缓存里面有");
            Subject subject = (Subject) redisUtils.getValue("adminSubject" + id);
            return Result.succ(200, "查询成功", subject);
        }else {
            System.out.println(id);
            Subject subject = subjectMapper.selectById(id);
            System.out.println(subject);
            if (subject != null) {
                redisUtils.saveValue("adminSubject" + id, subject);
                return Result.succ(200, "查询成功", subject);
            } else {
                return Result.fail("该学科不存在");
            }
        }
    }

    //学科编辑
    @PutMapping("/subject")
    @ApiOperation("编辑学科信息")
    public Result editSubjectInfo(@ModelAttribute Subject subject){
        UpdateWrapper UpdateQuery=new UpdateWrapper<>();
        UpdateQuery.eq("id",subject.getId());
        if(subject.getName()!=null){
        UpdateQuery.set("name",subject.getName());}
        if(subject.getLevel()!=null){
        UpdateQuery.set("level",subject.getLevel());}
        if(subject.getLevelName()!=null){
        UpdateQuery.set("level_name",subject.getLevelName());}
        int result=subjectMapper.update(null, UpdateQuery);
        if(result>0){
            //管理员登录的日志
            super.adminLog("admin修改了学科"+subject.getId()+"的内容");
            Subject subject1=subjectMapper.selectById(subject.getId());
            System.out.println(subject1);
            redisUtils.delAll();
            return Result.succ(200,"编辑成功",subject1);
        }else{
            return Result.fail("编辑失败");
        }
    }
    //学科删除
    @DeleteMapping("/subject/{id}")
    @ApiOperation("根据学科id删除学科")
    public Result DeleteSubjectById(@PathVariable("id") Integer id){
        int result =subjectMapper.deleteById(id);
        if(result>0){
            super.adminLog("admin 成功删除学科"+id+"信息");
            redisUtils.delAll();
            return Result.succ(200,"删除成功",null);
        }else{
            return Result.fail("删除失败");
        }
    }

    @PostMapping("/subject")
    @ApiOperation("学科创编")
    public Result addSubject(@RequestBody Subject subject){
        int result=subjectMapper.insert(subject);
        if(result>0){
            super.adminLog("admin 成功添加学科"+subject.getId()+"信息");
            redisUtils.delAll();
            return Result.succ(200,"添加成功",null);
        }else{
            return Result.fail("添加失败",null);
        }
    }

}

